﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Reflection;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MVCnExt.Web.UI.Ext
{
    public class ExtTreeNode : ExtConfig
    {
        private List<ConfigItem> _attributes = new List<ConfigItem>();

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            if (list.Count == 0)
                list.Add(new ExtScriptControlDescriptor("Ext.tree.TreeNode", "ctl00"));

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.tree.AsyncTreeNode";

            String[] properties = new String[]{
                "id", "leaf", "allowChildren", "allowDrag", "allowDrop", "cls", "disabled", "draggable", "expanded",
                "href", "hrefTarget", "iconFile", "icon", "iconCls", "isTarget", "qtip", "singleClickExpand",
                "text", "Checked"
            };

            Type component = typeof(ExtTreeNode);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtTreeNode.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }

            //add nodes
            if (children != null && children.Count != 0)
            {
                // build child tree
                StringBuilder tabList = new StringBuilder("[");

                for (int i = 0; i < children.Count; i++)
                {
                    // add comma between items
                    if (i != 0)
                    {
                        tabList.Append(",");
#if DEBUG
                        tabList.AppendLine("\t\t\t");
#endif
                    }

                    // get the item script
                    IList<ScriptDescriptor> descriptors = children[i].GetScriptDescriptors();

                    // write the item's config to the list
                    tabList.Append(((ExtScriptControlDescriptor)descriptors[0]).WriteScript(ScriptType.ConfigOnly));
                }

                tabList.Append("]");

                desc.AddPropertyBase("children", tabList);
            }

            return list;
        }

        #region Public Properties

        /// <summary>
        /// The content to be added to the Container
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(ExtContainer))]
        [TemplateInstance(TemplateInstance.Single)]
        [Description("The childrens of the tree.")]
        public List<ExtTreeNode> children { get; set; }

        /// <summary>
        /// Identifier value.
        /// </summary>
        [Description("Identifier value.")]
        public String id { get; set; }

        [DefaultValue(false)]
        [Description("True if this node is a leaf and does not have children")]
        public Boolean? leaf { get; set; }

        [DefaultValue(true)]
        [Description("False to not allow this node to have child nodes (defaults to true)")]
        public Boolean? allowChildren { get; set; }

        [DefaultValue(true)]
        [Description("False to make this node undraggable if draggable = true (defaults to true)")]
        public Boolean? allowDrag { get; set; }

        [DefaultValue(true)]
        [Description("False if this node cannot have child nodes dropped on it (defaults to true)")]
        public Boolean? allowDrop { get; set; }

        [DefaultValue("")]
        [Description("A css class to be added to the node.")]
        public String cls { get; set; }

        [DefaultValue(false)]
        [Description("True to start the node disabled")]
        public Boolean? disabled { get; set; }

        [DefaultValue(false)]
        [Description("True to make this node draggable (defaults to false)")]
        public Boolean? draggable { get; set; }

        [DefaultValue(false)]
        [Description("True to start the node expanded")]
        public Boolean? expanded { get; set; }

        [DefaultValue("#")]
        [Description("URL of the link used for the node (defaults to #)")]
        public String href { get; set; }

        [DefaultValue("")]
        [Description("Target frame for the link")]
        public String hrefTarget { get; set; }

        [DefaultValue("")]
        [Description("The path to an icon for the node. The preferred way to do this is to use the cls or iconCls attributes and add the icon via a CSS background image.")]
        public String iconFile { get; set; }

        [DefaultValue("")]
        [Description("The icon to use for the Node. See also, IconCls to set an icon with a custom Css class.")]
        public String icon { get; set; }

        [DefaultValue("")]
        [Description("A css class to be added to the nodes icon element for applying css background images")]
        public String iconCls { get; set; }

        [DefaultValue(true)]
        [Description("False to not allow this node to act as a drop target (defaults to true)")]
        public Boolean? isTarget { get; set; }

        [DefaultValue("")]
        [Description("An Ext QuickTip for the node")]
        public String qtip { get; set; }

        [DefaultValue(false)]
        [Description("True for single click expand on this node")]
        public Boolean? singleClickExpand { get; set; }

        [DefaultValue("")]
        [Description("The text for this node")]
        public String text { get; set; }

        [DefaultValue("")]
        [Description("True to render a checked checkbox for this node, false to render an unchecked checkbox (defaults to undefined with no..")]
        public Boolean? Checked { get; set; }

        #endregion
    }
}
